<?php

namespace App\Http\Controllers\Web;

use App\Models\Projects;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ProjectsController extends Controller
{
    public function index(Request $request)
    {
        $query = Projects::query()->with('product');
        $product_id = $request->input("product_id");
        if ($product_id) {
            $query->where('product_id', $product_id);
        }

        return $this->successPaginate($query->paginate(10));
    }

    public function requestData(Request $request)
    {
        $requestData = $this->validate($request, [
            'name' => 'required',
            'tag' => 'nullable',
            'desc' => "nullable",
            'desc_en' => "nullable",
            'address' => "nullable",
            'address_en' => "nullable",
            "img_list" => 'required',
            'introduction' => 'required',
            'detail' => 'nullable',
            'name_en' => 'nullable',
            'tag_en' => "nullable",
            'introduction_en' => 'nullable',
            "product_id" => 'required',
            'detail_en' => "nullable",
        ]);
        return $requestData;
    }

    public function show($id)
    {
        $team = Projects::query()->findOrFail($id);
        return $this->success($team);
    }

    public function store(Request $request)
    {
        $team = new Projects();
        $requestData = $this->requestData($request);
        $team->fill($requestData);
        $team->save();
        return $this->success($requestData);
    }

    public function update(Request $request, $id)
    {
        $team = Projects::query()->findOrFail($id);
        $requestData = $this->requestData($request);
        $team->fill($requestData);
        $team->save();
        return $this->success($requestData);
    }

    public function destroy($id)
    {
        $team = Projects::query()->findOrFail($id);
        $team->delete();
        return $this->success();
    }
}
